package com.android.openapp.services;



import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider{

	private static final String AUTHORITY = "com.openapp.cursorLoaders.contentprovider";
	public static final int NOTES = 100;
	public static final int NOTE_ID = 110;

	private static final String NOTE_BASE_PATH = DBHelper.TABLE_NAME;
	public static final Uri NOTE_CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/" + NOTE_BASE_PATH);

	public static final String NOTE_CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.example.places";
	public static final String NOTE_CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.example.placeelement";


	private static final UriMatcher sURIMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);
	static {
		sURIMatcher.addURI(AUTHORITY, NOTE_BASE_PATH, NOTES); //colleghiamo gli uri ai tipi, es. /notes = 100
		sURIMatcher.addURI(AUTHORITY, NOTE_BASE_PATH + "/#", NOTE_ID);
	}

	private SQLiteDatabase db; 

	@Override
	public boolean onCreate() {
		db = DBHelper.getHelper(getContext()).getWritableDatabase();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selArgs,
			String sortOrder) {

		String tableName = null;
		switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
		case NOTES: 
			tableName = DBHelper.TABLE_NAME;
			break;
		default:
			throw new IllegalArgumentException("Invalid Uri!");

		}

		Cursor cursor = db.query(tableName, projection, selection, 
				selArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);

		return cursor;
	}

	@Override
	public Uri insert(Uri uri, ContentValues cv) {
		String tableName = null;
		switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
		case NOTES: 
			tableName = DBHelper.TABLE_NAME;
			break;
		default:
			throw new IllegalArgumentException("Invalid Uri!");

		}

		long id = db.insert(tableName, null, cv);

		Uri retUri = ContentUris.withAppendedId(NOTE_CONTENT_URI, id);
		getContext().getContentResolver().notifyChange(retUri, null);

		return retUri;

	}

	@Override
	public int update(Uri uri, ContentValues values, String whereClause, String[] whereArgs) {
		String tableName = null;
		switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
		case NOTES: 
			tableName = DBHelper.TABLE_NAME;
			break;
		default:
			throw new IllegalArgumentException("Invalid Uri!");

		}

		int count = 0;
		count = db.update(tableName, values, whereClause, whereArgs);
		getContext().getContentResolver().notifyChange(uri, null);

		return count;
	}

	@Override
	public int delete(Uri uri, String whereClause, String[] whereArgs) {
		String tableName = null;
		switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
		case NOTES: 
			tableName = DBHelper.TABLE_NAME;
			break;
		default:
			throw new IllegalArgumentException("Invalid Uri!");

		}

		int count = 0;

		count = db.delete(tableName, whereClause, whereArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}


	@Override
	public String getType(Uri uri) {
		switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
		case NOTES: 
			return MyContentProvider.NOTE_CONTENT_TYPE;
		case NOTE_ID:
			return MyContentProvider.NOTE_CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Invalid Uri!");
		}
	}


}